function plusReady() {
    var goodsid = getUrlParam("goodsid");
    //加载订单详情
    LoadDetail(goodsid);
    //绑定事件
    BindEvent();
}
if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}
//$(document).ready(function(){
//	var goodsid = getUrlParam("goodsid");
//	LoadDetail(goodsid);
//});

//加载订单详情
function LoadDetail(goodsid) {

    var jqXHR = $.ajax({
        type: 'GET',
        url: configManager.RequstUrl + "api/goods/info?goods=" + goodsid
    });

    // 加载成功画页面
    jqXHR.done(function (data) {
        if (data.state != "success") { console.log(data.message); return; }
        var goodsInfo = data.data;
        var storeInfo = goodsInfo.store;
        goodsdetailinfo = goodsInfo;

        var html = [];
        // 商铺图片
        html.push("<div class='sjqbgdtu'><a><img width='398' height='212' src='" + configManager.goodsImgurl.format(goodsInfo.pic, "450-300") + "' /></a></div>");
        html.push("<div class='sjxqtpswz'>" + goodsInfo.title + "<br>" + storeInfo.store_name + "</div>");
        $("#goodspic").html(html.join(""));

        html = [];
        // 购买
        html.push("<div class='sjxqbkbg spxqcon'>");
        html.push("<p class='spxqljqg'><span>")

        if (storeInfo.store_pause) {
            //区分普通商品，团购商品
            if (1 == goodsInfo.goods_type) {
                html.push("<b class='spxqljan' id='buy'><a><i class='icon iconfont icon-refund' style='font-size:1.2em;'></i>立即抢购</a></b>");
            } else {
                html.push("<b class='spxqljan' id='buy'><a>立即购买</a></b><b class='spxqjran' id='addcart'><a>加入购物车</a></b>");
            }
        } else {
            html.push("<b class='spxqjrangray'><a>暂停营业</a></b>");
        }

        html.push("</span><b class='dhz'>" + goodsInfo.price + "元</b><font class='scz'>￥" + goodsInfo.market_price + "元</font>");
        html.push("</p>");

        html.push("<p class='sjxqsst spxqyhtb'>");

        if (goodsInfo.any_time_refund == 1) {
            html.push("<span><i class=\"checked checkbox icon\"></i>随时退</span>");
        }
        if (goodsInfo.over_time_refund == 1) {
            html.push("<span><i class=\"checked checkbox icon\"></i>过期退</span>");
        }
        html.push("<span><i class=\"truck icon\"></i>已售" + goodsInfo.selled + "件</span></p>");
        html.push("</div>");

        // 评分
        html.push("<div class='sjxqbkbg'>");
        html.push("<p class='sjxqsjpf'><span>0人评价</span><b>" + DrawScore(goodsInfo.score) + "</b></p>");
        html.push("</div>");

        // 预约电话
        html.push("<div class='sjxqbkbg'>");
        html.push("<p class='sjxqbddd spxqdhyy'><span><a href='tel:" + storeInfo.store_phone + "'><i class=\"phone sign icon\" style='font-size:3.2em; color: green'></i></a></span><b>预约电话<br />" + storeInfo.store_phone + "</b></p>");
        html.push("</div>");

        // 商家信息
        html.push("<div class='sjxqbkbg sjxqckxq'>");
        html.push("<h2>商家信息</h2>");
        html.push("<p style='line-height:2em; font-size:1em;'><a id='storedetail' tip='" + storeInfo.id + "'>" + storeInfo.store_name + "</a></p>");
        html.push("<p><i class=\"location basic icon\"></i>" + storeInfo.store_address + "</p>");
        html.push("</div>");

        // 商品描述
        html.push("<div class='sjxqbkbg sjxqckxq'>");
        html.push("<h2>本单详情</h2>");
        html.push("<p><b class='zcd'>" + goodsInfo.content + "</p>");
        html.push("<p style='line-height:30px' class='imagetext' class='sjxqtw'><a><span style='float:right'><i class=\"angle right icon\"></i></span>查看图文详情</a> </p>");
        html.push("</div>");

        // 购买须知
        html.push("<div class='sjxqbkbg sjxqckxq'>");
        html.push("<h2>购买须知</h2>");
        html.push("<p><b class='zcd'>" + goodsInfo.notice + "</p>");
        html.push("</div>");

        $("#goodsInfo").html(html.join(""));

    });

}

//绑定事件
function BindEvent() {

    //点击图文详情
    $("#goodsInfo").on("click", ".imagetext", function () {
        var page = "imagetext.html?goodsid=" + goodsdetailinfo.id;
        clicked(page);
    });

    // 商家详情
    $("#goodsInfo").on("click", "#storedetail", function () {

        clicked("../../merchant/detail.html?storeid=" + $(this).attr('tip'), false, false, 'pop-in');
    });

    //立即购买，只有一个商品，生成临时订单
    $("#goodsInfo").on("click", "#buy", function () {
        var goodinfo = goodsdetailinfo;
        //      console.log(JSON.stringify(goodinfo));
        var currentgoods = {
            "id": goodinfo.id,
            "storeid": goodinfo.store.id,
            "title": goodinfo.title,
            "price": goodinfo.price,
            "pic": goodinfo.pic,
            "count": 1,
            "selected": true
        };
        var temporder = { "goods": [currentgoods] };
        plus.storage.setItem(storageManager.temporder, JSON.stringify(temporder));
        var currentloginuser = JSON.parse(plus.storage.getItem(storageManager.user));
        if (!currentloginuser) {
            clicked("../../account/login.html", false, false, "slide-in-bottom");
        }
        clicked("../order/commit.html", false, false, "pop-in");
    });

    // 加入购物车
    $("#goodsInfo").on("click", "#addcart", function () {
        // 放入购物车缓存 
        var goodinfo = goodsdetailinfo;
        var currentgoods = {
            "id": goodinfo.id,
            "storeid": goodinfo.store.id,
            "title": goodinfo.title,
            "price": goodinfo.price,
            "pic": goodinfo.pic,
            "count": 1,
            "selected": true
        };

        var mycart = JSON.parse(plus.storage.getItem(storageManager.cart));
        if (!mycart) { mycart = []; }

        // 如果不是本商店商品，清空购物车
        $.each(mycart, function () {
            var goods = this;
            if (goods && goods.storeid != currentgoods.storeid) {
                plus.storage.removeItem(storageManager.cart);
            }
        });

        // 更新购物车
        var existsIndex = -1;
        $.each(mycart, function (index, goods) { if (goods && goods.id == currentgoods.id) { existsIndex = index; return false; } })
        if (existsIndex < 0) {
            mycart.push(currentgoods);
        } else {
            mycart[existsIndex].count = mycart[existsIndex].count + 1;
            mycart[existsIndex].price = currentgoods.price;
        }

        // 放入缓存，购物车，提交订单需要
        plus.storage.setItem(storageManager.cart, JSON.stringify(mycart));
        clicked("../order/cart.html", false, false, "pop-in");
    });
}


// 评分
//function DrawScore(score) {
//  score = (score < 5) ? score : 5;
//  var onscore = parseInt(score);
//  var offscore = parseInt(5 - score);
//
//  var html = [];
//  for (var i = 0; i < onscore; i++) {
//      html.push("<img src='../../../images/star-on-big.png' height='19' width='19' />");
//  }
//  if ((offscore + onscore) < 5) {
//      html.push("<img src='../../../images/star-on-haft.png' height='19' width='19' />");
//  }
//  for (var i = 0; i < offscore; i++) {
//      html.push("<img src='../../../images/star-off-big.png' height='19' width='19' />");
//  }
//  return html.join("");
//}

function DrawScore(score) {
    var pic = '0';
    if (score > 1 && score < 2) {
        pic = '1';
    } else if (score > 2 && score < 3) {
        pic = '2';
    } else if (score == 3) {
        pic = '3';
    } else if (score > 3 && score < 4) {
        pic = '35';
    } else if (score == 4) {
        pic = '4';
    } else if (score > 4 && score < 5) {
        pic = '45';
    } else if (score == 5) {
        pic = '5';
    } else {
        pic = '0';
    }
    return '<img style="vertical-align:middle; margin-top:5px" width="90" height="18" src="http://i3.dpfile.com/s/i/app/api/32_' + pic + 'star.png" />';
}